<template>
    <div>
<el-breadcrumb separator-class="el-icon-arrow-right">
  <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
  <el-breadcrumb-item>商品管理</el-breadcrumb-item>
  <el-breadcrumb-item>商品分类</el-breadcrumb-item>
</el-breadcrumb><br/><br/>
<el-card class="box-card">
  <el-button type='primary' style='float:left' @click='isshow=!isshow'>添加分类</el-button>
  <br/>
  <el-table
    :data="cate"
    style="width: 100%" row-key="cat_id" :tree-props="{children:'children',hasChildren:'hasChildren'}">
    <el-table-column
      type='index'
      label="序号"
      width="150">
    </el-table-column>
    <el-table-column
      prop="cat_name"
      label="分类名称"
      width="150">
    </el-table-column>
    <el-table-column
      prop="cat_deleted"
      label="是否有效"
      width="150">
      <template slot-scope='scope'>
           <span v-if='!scope.row.cat_deleted' class="el-icon-success" style="color:#13c920"></span>
           <span v-else class="el-icon-error" style="color:red"></span>
      </template>
    </el-table-column>
    <el-table-column
      prop="cat_pid"
      label="分类排序"
      width="150">
      <template slot-scope='scope'>
          <el-tag v-if="scope.row.cat_level==0">一级</el-tag>
          <el-tag v-if="scope.row.cat_level==1" type="success">二级</el-tag>
          <el-tag v-if="scope.row.cat_level==2" type="warning">三级</el-tag>
      </template>
    </el-table-column>
    <el-table-column
      fixed="right"
      label="操作">
      <template slot-scope="scope">
        <el-button type="primary" @click='edit(scope.row)' icon="el-icon-edit">编辑</el-button>
        <el-button type="danger" @click='del(scope.row.cat_id)' icon="el-icon-delete">删除</el-button>
      </template>
    </el-table-column>
  </el-table>

<!-- 添加分类 -->
<el-dialog title="添加分类" :visible.sync="isshow" @close='close' @open='getcate2'>
  <el-form :model="form" ref="addCate">
    <el-form-item label="分类名称" prop='cat_name'>
      <el-input v-model="form.cat_name"></el-input>
    </el-form-item>
    <el-form-item label="父级分类">
      <el-cascader
    v-model="value"
    :options="cate2"
    @change='set'
     :props="{value:'cat_id',label:'cat_name',checkStrictly:true}"
    ></el-cascader>
    </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="isshow = false">取 消</el-button>
    <el-button type="primary" @click='addCate'>确 定</el-button>
  </div>
</el-dialog>

<!-- 编辑分类 -->
<el-dialog title="收货地址" :visible.sync="editshow">
  <el-form :model="form" ref="editcate">
    <el-form-item label="活动名称" >
      <el-input v-model="form.cat_name" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="父级分类" >
      <el-cascader
    :options="cate2"
    @change='set'
     :props="{value:'cat_id',label:'cat_name',checkStrictly:true}"
    ></el-cascader>
    </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="editshow = false">取 消</el-button>
    <el-button type="primary" @click='editcate'>确 定</el-button>
  </div>
</el-dialog>

<div class="block">
    <span class="demonstration">完整功能</span>
    <el-pagination
      @size-change="changeSize"
      @current-change="changePage"
      :current-page="page.pagenum"
      :page-sizes="[1, 5, 10, 20]"
      :page-size="page.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="count">
    </el-pagination>
  </div>


</el-card>


    </div>
</template>

<script>
import {
  categories,
  addcategories,
  editcategories,
  delcategories
} from "../api/api.js";
export default {
  data() {
    return {
      cate: [],
      isshow: false,
      editshow: false,
      cate2: [],
      value: [],
      count: 0,
      page: {
        pagenum: 1,
        pagesize: 5,
      },
      edit_id: 0,
      form: {
        cat_name: "",
        cat_pid: 0,
        cat_level: 0
      }
    };
  },
  mounted() {
    this.getcate();
    this.getcate2();
  },
  methods: {
    async getcate() {
      console.log("xcc",this.page);
      let res = await categories(this.page);
      console.log(res);
      this.cate = res.data.result;
      this.count=res.data.total
    },
    async getcate2() {
      let res = await categories(2);
      console.log("xxx", res);
      this.cate2 = res.data;
    },
    set(data) {
      console.log(data);
      this.form.cat_pid = data[data.length - 1];
      this.form.cat_level = data.length;
    },
    addCate() {
      this.$refs["addCate"].validate(async valid => {
        if (valid) {
          let res = await addcategories(this.form);
          console.log(res);
          if (res.meta.status == 201) {
            this.$message.success(res.meta.msg);
            this.isshow = !this.isshow;
            this.getcate();
          }
        } else {
          return false;
        }
      });
    },
    edit(row) {
      this.editshow = !this.editshow;
      this.form = {
        cat_name: row.cat_name,
        cat_pid: row.cat_pid
      };
      this.edit_id = row.cat_id;
    },
    editcate() {
      this.$refs["editcate"].validate(async valid => {
        if (valid) {
          let res = await editcategories(this.edit_id, this.form);
          console.log(res);
          if (res.meta.status == 200) {
            this.$message.success(res.meta.msg);
            this.getcate();
          }
        } else {
          return false;
        }
      });
    },
    del(id) {
      this.$confirm("确定要删除该分类吗").then(async () => {
        let res=await delcategories(id);
        console.log(res)
        if(res.meta.status==200){
          this.$message.success(res.meta.msg)
          this.getcate();
        }
      });
    },

    changePage(pagenum){
      this.page.pagenum=pagenum
      this.getcate();
    },
    changeSize(pagesize){
        this.page.pagesize=pagesize;
        this.getcate();
    },
    close(){
      this.form={
        cat_name:'',
        cat_pid:0,
        cat_level:0
      };
      this.value=[]
    }
  }
};
</script>

<style lang='scss' scoped >
</style>
